const gulp = require("gulp");
const cssmin = require("gulp-cssmin");
const autoprefixer = require("gulp-autoprefixer");
const uglify = require("gulp-uglify");
const babel = require("gulp-babel");
const htmlmin = require("gulp-htmlmin");
const webserver = require("gulp-webserver");
const clean = require("gulp-clean");
const sass = require("gulp-sass-china");

// static的转存
function copyFileFn(){
    return gulp.src("./ule_project/static/**/*")
        .pipe(gulp.dest("./server/static"))
}
exports.copyFile = copyFileFn; 

// libs的转存
function libsFn(){
    return gulp.src("./ule_project/libs/**/*")
        .pipe(babel({
            presets:["@babel/env"]
        }))
        .pipe(uglify())
        .pipe(gulp.dest("./server/libs"))
}
exports.libs = libsFn; 

// iconfont的转存
function iconFn(){
    return gulp.src("./ule_project/iconfont/**/*")
        .pipe(gulp.dest("./server/iconfont"))
}
exports.icon = iconFn; 

// sass文件的处理pages的css
function psassFn(){
    return gulp.src("./ule_project/sass/pagescss/**/*")
            .pipe(sass())
            .pipe(gulp.dest("./ule_project/css"))
}
exports.psass = psassFn;

// 处理css的指令：压缩，添加前缀
function cssFn(){
    return gulp.src("./ule_project/css/**/*")
        .pipe(autoprefixer("last 2 version","safari 5","ie 8","ie 9","opera 12.1","ios 6","android 4"))
        .pipe(cssmin())
        .pipe(gulp.dest("./server/css"))
}
exports.css = cssFn;


// sass文件的处理pubcss的css
function sassFn(){
    return gulp.src("./ule_project/sass/pubcss/**/*")
            .pipe(sass())
            .pipe(gulp.dest("./ule_project/public"))
}
exports.sass = sassFn;


// 处理public的css的指令：压缩，添加前缀
function pcssFn(){
    return gulp.src("./ule_project/public/**/*")
        .pipe(autoprefixer("last 2 version","safari 5","ie 8","ie 9","opera 12.1","ios 6","android 4"))
        .pipe(cssmin())
        .pipe(gulp.dest("./server/public"))
}
exports.pcss = pcssFn;

exports.abc = gulp.series(sassFn,pcssFn);
function wat(qwe){
    gulp.watch("./ule_project/sass/pubcss/**/*",sassFn);
    gulp.watch("./ule_project/public/**/*",pcssFn);

    qwe();
}
exports.wat = wat;


// 处理js的指令：ES6编译ES5，压缩
function jsFn(){
    return gulp.src("./ule_project/js/**/*")
        .pipe(babel({
            presets:["@babel/env"]
        }))
        .pipe(uglify())
        .pipe(gulp.dest("./server/js"))
}
exports.js = jsFn;

// 处理html的指令：压缩
function htmlFn(){
    return gulp.src("./ule_project/pages/**/*")
        .pipe(htmlmin({
            removeEmptyAttributes:true,
            collapseWhitespace:true
        }))
        .pipe(gulp.dest("./server/pages"))
}
exports.html = htmlFn;

// 首页的转存
function indexFn(){
    return gulp.src("./ule_project/index.html")
            .pipe(htmlmin({
                removeEmptyAttributes:true,
                collapseWhitespace:true
            }))
            .pipe(gulp.dest("./server/"))
}
exports.index = indexFn;

// 定义服务器功能
function serverFn(){
    return gulp.src("./server")
        .pipe(webserver({
            host:"localhost",
            port:"666",
            livereload:true,
            open:"./index.html",
        }))
}
exports.server = serverFn;

// 批量转存
exports.copyAll = gulp.series(htmlFn,jsFn,psassFn,sassFn,pcssFn,cssFn,copyFileFn,iconFn,libsFn,indexFn);

// 监听
function watchAllFn(qwe){
    gulp.watch("./ule_project/index.html",indexFn);
    gulp.watch("./ule_project/sass/pagescss/**/*",psassFn);
    gulp.watch("./ule_project/sass/pubcss/**/*",sassFn);
    gulp.watch("./ule_project/css/**/*",cssFn);
    gulp.watch("./ule_project/public/**/*",pcssFn);
    gulp.watch("./ule_project/js/**/*",jsFn);
    gulp.watch("./ule_project/pages/**/*",htmlFn);
    gulp.watch("./ule_project/static/**/*",copyFileFn);
    gulp.watch("./ule_project/libs/**/*",libsFn);
    gulp.watch("./ule_project/iconfont/**/*",iconFn);

    qwe();
}

exports.watchAll = watchAllFn;


// 开启服务器并监听
exports.all = gulp.series(
    
    gulp.series(htmlFn,jsFn,psassFn,sassFn,pcssFn,cssFn,copyFileFn,iconFn,libsFn,indexFn),
     gulp.parallel(watchAllFn,serverFn)
);



